import sys 
sys.path.append("../")
from UI.register_ui import Ui_MainWindow as Register
from PyQt5.QtWidgets import QMainWindow
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox
from database.database import DataBase
class Main_Ui(QMainWindow, Register):
    def __init__(self, database_path):
        super(Main_Ui, self).__init__()
        self.setupUi(self)
        self.pushButtonLogon.clicked.connect(self.InsertDatabase)
        self.database = DataBase(database_path)

    def InsertDatabase(self):
        username = self.lineEdit.text()
        password = self.lineEdit_2.text()

        try:
            new_user = (username, password)
            mark = self.database.insert_user(new_user)
            if mark:
                QMessageBox.warning(None, '消息','注册成功！', QMessageBox.Yes)
                self.close()

            else:
                QMessageBox.warning(None, "消息", "用户名已存在", QMessageBox.Yes)
        except Exception as e:
            print(e)
            print("数据查询错误！")
         

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ui = Main_Ui("../database/plave_data.db")
    ui.show()
    sys.exit(app.exec_())
